package com.cocostudio.erpapp;

import java.math.BigInteger;

import org.ksoap2.serialization.SoapObject;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.cocostudio.erpapp.CustomDialogFragment.CustomDialogInfo;
import com.cocostudio.erpapp.db.DBHelper;
import com.cocostudio.erpapp.provider.Settings;
import com.cocostudio.erpapp.utils.UpdateManager;
import com.cocostudio.erpapp.vo.WSLoginResult;
import com.cocostudio.erpapp.R;

public class MainActivity extends CustomFragmentActivity {
    private static final String TAG="MainActivity";
    private static final String ABOUNT_TEXT = "进销存查询系统<br>Android版 1.0版<br><br>网站 : <a href='http://www.xxx.cn'>www.xxxx.com.cn</a><br>联系电话：<a href='tel:010-xxxxx'>010-8xxxx77</a>";

    public static WSLoginResult curlogin = null;

    private TextView titleview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitleBarVisible(false);

        titleview= (TextView)findViewById(R.id.textView1);


        if (Settings.isFirstTimeLaunch(this)) {
            CustomDialogInfo info = new CustomDialogInfo();
            info.icon = android.R.drawable.ic_dialog_info;
            info.title = R.string.msg_shortcut_confim;
            info.showNegativeButton = true;
            info.negativeButtonText = android.R.string.no;
            info.positiveButtonText = android.R.string.yes;
            info.positiveButtonListener = new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    addShortcut();
                }
            };
            CustomDialogFragment.show(getSupportFragmentManager(), info);
            Settings.overFirstTimeLaunched(this);
        }

        if ( curlogin !=null)
        {

            titleview.setText(
                    getString(R.string.title,  "",curlogin.getPersonName())
                    );
        }
        checkNetworkState();
    }

    private void checkNetworkState() {
        ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo ni = manager.getActiveNetworkInfo();
        if (ni == null) {
            CustomDialogInfo info = new CustomDialogInfo();
            info.icon = android.R.drawable.ic_dialog_alert;
            info.title = R.string.label_prompt;
            info.message = R.string.msg_no_network;
            info.showNegativeButton = true;
            info.negativeButtonText = android.R.string.cancel;
            info.positiveButtonText = R.string.label_setting;
            info.positiveButtonListener = new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    CustomDialogFragment.dismiss(getSupportFragmentManager());
                    Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
                    startActivity(intent);
                }
            };
            CustomDialogFragment.show(getSupportFragmentManager(), info);
        }
    }

    private void addShortcut() {
        Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
        shortcut.putExtra("duplicate", false);
        ComponentName comp = new ComponentName(this, SplashActivity.class);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
        ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
        sendBroadcast(shortcut);
    }

    public void testWebService(){

        SoapObject  request = new SoapObject("http://xxxxxservice", "getName");

        request.addProperty("ProperyName", "ProperyValue");


       // request.getres
    }

    public void onAboutClicked(View v) {
        CustomDialogInfo info = new CustomDialogInfo();
        info.title = R.string.title_about;
        LayoutInflater inflater = getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_about, null);
        TextView textView = (TextView) view.findViewById(R.id.text);
        textView.setText(Html.fromHtml(ABOUNT_TEXT));
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        info.view = view;
        info.positiveButtonText = R.string.label_share;
        info.positiveButtonListener = new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.label_share));
                intent.putExtra(Intent.EXTRA_TEXT, getString(R.string.msg_about));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(Intent.createChooser(intent, getTitle()));
            }
        };
        CustomDialogFragment.show(getSupportFragmentManager(), info);
    }

    @Override
    protected void onResume() {
        //MobclickAgent.onResume(this);
        super.onResume();
    }

    @Override
    protected void onPause() {
       // MobclickAgent.onPause(this);
        super.onPause();
    }

    public void onItemClicked(View v) {
        Intent i = null;
//        i= new Intent(this, RealTimeSalesActivity.class);
        switch (v.getId()) {
        case R.id.exhibition_details:
            //i = new Intent(this, ExhibitionDetailsActivity.class);
            break;
        case R.id.schedule:
            //i = new Intent(this, SchedulesActivity.class);
            break;
        case R.id.contact_us:
           // i = new Intent(this, ContactUsActivity.class);
            break;
        case R.id.realtime_sales:
            i = new Intent(this, RealTimeSalesActivity.class);
            break;
        case R.id.realtime_stock:
            i = new Intent(this, RealTimeStockActivity.class);
            break;
        case R.id.history_sales:
            i = new Intent(this, HistorySalesActivity.class);
            break;
//        case R.id.dept_history_sale:
//            i = new Intent(this, DeptHistorySalesActivity.class);
//            break;
        case R.id.getgoods_info:
            i = new Intent(this, GetGoodsInfoActivity.class);
            break;

        case R.id.realtime_payments:
            i = new Intent(this, RealTimePaymentsActivity.class);
            break;

        case R.id.guid:
            UpdateManager uman=new UpdateManager(this);
            uman.checkUpdateInfo();

            break;
        case R.id.news:
            //i = new Intent(this, NewsActivity.class);
            break;
        case R.id.my_exhibitions:
            //i = new Intent(this, MyExhibitionsActivity.class);
            break;
        }

        if (i != null) {
            startActivity(i);
        }
    }

    /**
     * 返回当前程序版本名
     */

    public static String getRemoteAppVersion(){

        return "";
    }
    public static String getAppVersionName(Context context) {
        String versionName = "";
        try {
            // ---get the package info---
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = pi.versionName;
            if (versionName == null || versionName.length() <= 0) {
                return "";
            }
        } catch (Exception e) {
        }
        return versionName;
    }

    public static int getAppVersionCode(Context context) {
        int versioncode = 0;
        try {
            // ---get the package info---
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            return pi.versionCode;
        } catch (Exception e) {
        }
        return versioncode;
    }


    @Override
    public void onBackPressed() {
        CustomDialogInfo info = new CustomDialogInfo();
        info.icon = android.R.drawable.ic_dialog_alert;
        info.title = R.string.msg_exit_confirm;
        info.showNegativeButton = true;
        info.negativeButtonText = android.R.string.no;
        info.positiveButtonText = android.R.string.yes;
        info.positiveButtonListener = new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        };
        CustomDialogFragment.show(getSupportFragmentManager(), info);
    }

    public void openAd(View v) {
//        Uri uri = Uri.parse("http://www.sohu.com.cn");
//        Intent it = new Intent(Intent.ACTION_VIEW, uri);
//        startActivity(it);
    }

    @Override
    protected void onDestroy() {
        DBHelper.closeDB();
        super.onDestroy();
    }

}
